package com.runa.rccprotocol.dto.tiangang;

import com.runa.protocol.dto.IResponseData;
import com.runa.protocol.util.ByteUtil;
import com.runa.rccprotocol.dto.ConcentratorRequestData;

/**
 * @author diandian
 * @Date 2021/12/21 12:54
 */
public class TianGangFmuRequestData extends ConcentratorRequestData {
    private byte[] requestData;

    public TianGangFmuRequestData(String address) {
        super(address);
    }

    @Override
    public boolean check(IResponseData data) {
        return true;
    }

    @Override
    public byte[] getData() {
        return requestData;
    }

    public void init() {
        TianGangValveRequestData re = new TianGangValveRequestData(this.address);
        re.init();
        byte[] data = re.getData();
        this.fmuBytes(data);
        setHaveResult(true);
    }

    public void chooseHouseValve(String valve) {
        TianGangValveRequestData re = new TianGangValveRequestData(this.address);
        re.chooseHouseValve(valve);
        byte[] data = re.getData();
        this.fmuBytes(data);
        setHaveResult(true);
    }

    public void setHouseValveOpen(int open) {
        TianGangValveRequestData re = new TianGangValveRequestData(this.address);
        re.setHouseValveOpen(open);
        byte[] data = re.getData();
        this.fmuBytes(data);
        setHaveResult(true);
    }

    public void readHouseValve() {
        TianGangValveRequestData re = new TianGangValveRequestData(this.address);
        re.readHouseValve();
        byte[] data = re.getData();
        this.fmuBytes(data);
        setHaveResult(true);
    }

    public void destroy() {
        TianGangValveRequestData re = new TianGangValveRequestData(this.address);
        re.destroy();
        byte[] data = re.getData();
        this.fmuBytes(data);
    }

    private void fmuBytes(byte[] data) {
        int length = data.length + 6;
        requestData = new byte[length];
        requestData[0] = (byte) 0xF8;
        requestData[1] = 0x01;
        requestData[2] = 0x09;
        requestData[3] = (byte) data.length;
        for (int i = 0; i < data.length; i++) {
            requestData[i+4] = data[i];
        }
        requestData[length - 2] = ByteUtil.sum(requestData, 0, length - 2);
        requestData[length - 1] = 0x16;
    }

}